package com.jianzh5.action.web;

import com.jianzh5.action.BaseAction;
import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.pojo.PAdmin;
import com.jianzh5.pojo.PRole;
import com.jianzh5.service.RoleServiceI;
import com.jianzh5.service.UserServiceI;
import com.jianzh5.util.InfoCenterHome;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台管理员控制器 ,权限控制到页面
 * @author jianzh5
 */
@Controller
@RequestMapping("/sys/user")
public class UserAction extends BaseAction{
	
	@Autowired
	private UserServiceI userService;
	@Autowired
	private RoleServiceI roleService;
	
	@RequiresPermissions("sys:user:view")
	@RequestMapping("/userPage")
	public String userPage(){

        return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/userList";
	}
	
	/**
	 * 返回后台管理员列表
	 * @param datapage
	 * @param request
	 * @return
	 */
	@RequiresPermissions("sys:user:view")
	@RequestMapping(value = "/userList",method = RequestMethod.POST)
	@ResponseBody
	public DataTables userList(DataPage datapage, HttpServletRequest request){
        logger.info("处理异常错误...");
		String sindex = request.getParameter("order[0][column]");
		String order = request.getParameter("order[0][dir]");
		String sort = request.getParameter("columns["+sindex+"][data]");
		DataTables dt = userService.dataTables(order,sort,datapage);
		return dt;
	}
	
	/**
	 * 跳转后台管理用户编辑页面
	 * @param id
	 * @return
	 */
	@RequestMapping("/editUserPage")
	@RequiresPermissions("sys:user:edit")
	public String userEditPage(@RequestParam(value = "id") int id,Model model){
		PAdmin puser = userService.getPadminWithRole(id);
		//查询系统角色列表，可将其存储在缓存中以备下次使用
		List<PRole> roleList = roleService.getRoleList();
		model.addAttribute("puser", puser);
		model.addAttribute("roleList",roleList);
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/userEdit";
	}
	
	/**
	 * 编辑管理员
	 * @return
	 */
	@RequiresPermissions("sys:user:edit")
	@RequestMapping("/userEdit")
	@ResponseBody
	public ResultJson userEdit(PAdmin admin){
		ResultJson json = new ResultJson();
		try{
			userService.editUser(admin);
			json.setSuccess(true);
			json.setMsg("用户编辑成功...");
		}catch (Exception e) {
			json.setMsg("用户编辑异常...");
		}
		return json;
	}
	
	
	@RequiresPermissions("sys:user:add")
	@RequestMapping("/userAddPage")
	public String userAddPage(Model model){
		//查询系统角色列表，可将其存储在缓存中以备下次使用
		List<PRole> roleList = roleService.getRoleList();
		model.addAttribute("roleList",roleList);
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/sys/userAdd";
	}
	
	@RequestMapping("/userAdd")
	@RequiresPermissions("sys:user:add")
	@ResponseBody
	public ResultJson userAdd(PAdmin admin){
		ResultJson json = new ResultJson();
		try{
			userService.addUser(admin);
			json.setSuccess(true);
			json.setMsg("管理员添加成功...");
		}catch (Exception e) {
			json.setMsg("管理员添加失败,数据库异常...");
		}
		return json;
	}
	
	
	@RequestMapping("/userDel")
	@RequiresPermissions("sys:user:del")
	@ResponseBody
	public ResultJson userDel(@RequestParam(value = "userId") int userId){
		ResultJson json = new ResultJson();
		try{
			userService.delUser(userId);
			json.setSuccess(true);
			json.setMsg("管理员刪除成功...");
		}catch (Exception e) {
			json.setMsg("管理员刪除失败,数据库异常...");
		}
		return json;
	}
	
	
	@RequestMapping("/checkUserName")
	@ResponseBody
	public boolean checkUserName(String username,String oldusername){
		if(username != null && username.equals(oldusername)){
			return true;
		}else if(username != null && userService.getByAccount(username) == null){
			return true;
		}
		return false;
	}
	
}
